package com.huanmeiqi.demo.server.consumer.controller;

import com.huanmeiqi.demo.api.domain.TOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author cq.Wang
 * @date 2018/6/12 15:26
 * @description
 */
@RestController
@RequestMapping(value = "orders")
public class OrderConsumerController {

    /**
     * 第一版，无负载均衡，写死一个服务提供者地址，譬如8081端口
     */
//    private static final String REST_URL_PREFIX = "http://localhost:8081/";

    /**
     * 第2版，无负载均衡，写死一个服务提供者地址，譬如8081端口
     */
    private static final String REST_URL_PREFIX = "http://SERVER-PROVIDER/";

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("{orderId}")
    public TOrder getOrder(@PathVariable Long orderId) {
        // 请求有三个重载方法:占位符、Map传参数、
        ResponseEntity<TOrder> forEntity = restTemplate.getForEntity(REST_URL_PREFIX + "orders/{1}", TOrder.class, orderId);
        TOrder tOrder = forEntity.getBody();
        return tOrder;
    }

}
